<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Setting;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class SettingController extends CommonController
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function getIndex()
    {
        $lists = Setting::orderBy('id','asc')->paginate(10);
        return view('settins.index')->with("lists",$lists)->with("actions",$this->actions);
    }
    public function search(Request $request)
    {
        $data = $request->input();
        $sql = Setting::orderBy('id','asc');
        if(isset($data['name']) && $data['name'] !='')
        {
            $sql->where('name','like','%'.trim($data['name']).'%');
        }
        if(isset($data['variable']) && $data['variable'] !='')
        {
            $sql->where('variable','like','%'.trim($data['variable']).'%');
        }
        if(isset($data['value']) && $data['variable'] !='')
        {
            $sql->where('variable','like','%'.trim($data['variable']).'%');
        }
        if(isset($data['variable']) && $data['variable'] !='')
        {
            $sql->where('variable','like','%'.trim($data['variable']).'%');
        }
        $lists = $sql->paginate(10);
        return view('setting.search')->with('lists',$lists)
            ->with('name',$data['name'])
            ->with('value',$data['value'])
            ->with("variable", $data['variable'])
            ->with("system", $data['system'])
            ->with("actions", $this->actions);
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */

    public function getEdit(Request $request)
    {
        $data = $request->input();
        $me = Setting::where('id',$data['id']->first());
        return view('setting.edit')->with('me',$me);
    }
    public function postEdit(SettingRequest $request)
    {
        $data = $request->input();
        $id=$data['id'];
        unset($data['id'],$data['_token'],$data['formType']);
        Setting::where('id', $id)->update($data);
        Setting::cache(1);
        return AjaxCallbackMessage('恭喜，修改成功',true,url('setting/index'));
    }
   public function getCreate()
   {
       return view('setting.create');
   }
    public function postCreate(SettingRequest $request)
    {
        $data = $request->input();
        unset($data['_token'],$data['formType']);
        $result=Setting::create('$data');
        Setting::cache(1);
        return AjaxCallbackMessage('恭喜,创建成功，新分类的id为' . $result->id, true, url('setting/index'));
    }
    public function postDelete(Request $request)
    {
        $id = $request->input('sid');
        Setting::where('id',$id)->delete();
        Setting::cache(1);
        return AjaxCallbackMessage('恭喜删除成功',true,url('setting/index'));

    }
}
